# -*- coding: utf-8 -*-
# @Time    : 2021/10/26 8:53
# @Author  : TangDagui
# @Email   : 3297364807@qq.com
# @File    : AlgorithmDome.py
# @Software: PyCharm
# @Question: 算法第一题：搜索二维数组
import platform
import time

platform.system()
matrix = [[1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30]]
count = 30
print(matrix)


# 第一种方法爆破查找
def blasting(number):
    for i in matrix:
        for j in i:
            if j == number:
                return True
    return False
    pass


# 第二种方法二分查找  有问题 待修改
def binarySearch(param):
    arrayLength = len(matrix[0])
    for i in matrix:
        length = int(len(matrix[0]) / 2)  # 切半
        if param > i[length]:  # 如果查找的值大于折半的值
            while length < arrayLength:  # 每次加1个数
                if param == i[length]:
                    return True
                length += 1
        elif param < i[length]:  # 如果查找的值小于折半的值
            while length >= 0:  # 每次减1个数
                if param == i[length]:
                    return True
                length -= 1
        elif param == i[length]:  # 如果查找的值大于折半的值
            return True

    return False
    pass


def zigzagSearch():
    m = len(matrix)  # 数组长度
    n = len(matrix[0])  # 二维数组长度
    i, j = 0, n - 1
    while i < m and j >= 0:
        if matrix[i][j] == count:
            return True
        elif matrix[i][j] < count:
            i += 1
        elif matrix[i][j] > count:
            j -= 1
    return False
    pass


time_start = time.perf_counter()
# 第一种方法爆破查找
print(blasting(count))
time_end = time.perf_counter()
print("运行时间：{}".format((time_end - time_start)))

# 第二种方法二分查找
time_start = time.perf_counter()
print(binarySearch(count))
time_end = time.perf_counter()
print("运行时间：{}".format((time_end - time_start)))

# 第三种方法 z字形查找
time_start = time.perf_counter()

print(zigzagSearch())
time_end = time.perf_counter()
print("运行时间：{}".format((time_end - time_start)))
